package cfdt.ldl.signatrue.config;

import cfdt.common.enums.CommonCodeEnum;
import cfdt.common.utils.nacos.NacosConfigLocalCatch;
import cfdt.common.exception.CodeException;
import cfdt.tools.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 初始化
 *
 * <pre>
 * Modify Information:
 * Author       Date          Description
 * ============ ============= ============================
 * lsp        2020年12月24日    Create this file
 * </pre>
 */
@Component
public class SystemEnvironment {

    public static final Logger logger = LoggerFactory.getLogger("system");

    @Autowired
    private NacosConfigLocalCatch nacosConfigLocalCatch;

    public String loadBusiValue(String pncode, String dbcode) throws CodeException {
        @SuppressWarnings("unchecked")
        Map<String, Map<String, Map<String, String>>> map = nacosConfigLocalCatch.get("EGPSignature-Cert.json", Map.class);
        String cert = null;
        if (StringUtils.isNotEmpty(dbcode) && map.get(pncode).get(dbcode) != null) {
            cert = map.get(pncode).get(dbcode).get("secretKey");
        }
        if (StringUtils.isEmpty(cert)) {
            cert = map.get(pncode).get("default").get("secretKey");
        }
        if (StringUtils.isEmpty(cert)) {
            logger.error("未找到配置信息[pncode={},dbcode={}]", pncode, dbcode);
            throw new CodeException(CommonCodeEnum.CODE_2470);
        }
        return cert;
    }

    public String loadBusiValue(String pncode, String dbcode, String flag) throws CodeException {
        @SuppressWarnings("unchecked")
        Map<String, Map<String, Map<String, String>>> map = nacosConfigLocalCatch.get("EGPSignature-Cert.json", Map.class);
        String cert = null;
        if (StringUtils.isNotEmpty(dbcode) && map.get(pncode).get(dbcode) != null) {
            if ("public".equals(flag)) {
                cert = map.get(pncode).get(dbcode).get("publicKey");
            } else if ("private".equals(flag)) {
                cert = map.get(pncode).get(dbcode).get("privateKey");
            }
        }
        if (StringUtils.isEmpty(cert)) {
            if ("public".equals(flag)) {
                cert = map.get(pncode).get("default").get("publicKey");
            } else if ("private".equals(flag)) {
                cert = map.get(pncode).get("default").get("privateKey");
            }
        }
        if (StringUtils.isEmpty(cert)) {
            logger.error("未找到配置信息[pncode={},dbcode={}]", pncode, dbcode);
            throw new CodeException(CommonCodeEnum.CODE_2470);
        }
        return cert;
    }

    public String loadBusiValueByfield(String pncode, String dbcode, String field) throws CodeException {
        @SuppressWarnings("unchecked")
        Map<String, Map<String, Map<String, String>>> map = nacosConfigLocalCatch.get("EGPSignature-Cert.json", Map.class);
        String cert = null;
        if (StringUtils.isNotEmpty(dbcode) && map.get(pncode).get(dbcode) != null) {
            cert = map.get(pncode).get(dbcode).get(field);
        }
        if (StringUtils.isEmpty(cert)) {
            logger.error("未找到配置信息[pncode={},dbcode={}]", pncode, dbcode);
            throw new CodeException(CommonCodeEnum.CODE_2470);
        }
        return cert;
    }

}